using System;
using System.Threading.Tasks;
using ERP.EquipmentManage;
using ERP.EquipmentManage.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Volo.Abp.AspNetCore.Mvc;

namespace ERP.Web.Controllers
{
    /// <summary>
    /// 维修单控制器
    /// </summary>
    public class RepairController : AbpController
    {
        private readonly IRpeairService _repairService;
        private readonly ILogger<RepairController> _logger;

        public RepairController(
            IRpeairService repairService,
            ILogger<RepairController> logger)
        {
            _repairService = repairService;
            _logger = logger;
        }

        /// <summary>
        /// 维修单管理页面
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 维修单编辑页面
        /// </summary>
        public IActionResult Edit()
        {
            return View();
        }

        /// <summary>
        /// 获取维修单分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetList(GetRepairInputDto input)
        {
            try
            {
                var result = await _repairService.GetListAsync(input);
                return Json(new
                {
                    code = 0,
                    msg = "",
                    count = result.TotalCount,
                    data = result.Items
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取维修单列表失败");
                return Json(new { code = 1, msg = ex.Message });
            }
        }

        /// <summary>
        /// 获取维修单详情
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetRepair(int id)
        {
            try
            {
                var result = await _repairService.GetAsync(id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取维修单详情失败，ID: {Id}", id);
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 创建维修单
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> CreateRepair(CreateRepairDto input)
        {
            try
            {
                var result = await _repairService.CreateAsync(input);
                _logger.LogInformation("创建维修单成功，ID: {Id}", result.Id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建维修单失败");
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 更新维修单
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> UpdateRepair(int id, UpdateRepairDto input)
        {
            try
            {
                var result = await _repairService.UpdateAsync(id, input);
                _logger.LogInformation("更新维修单成功，ID: {Id}", id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新维修单失败，ID: {Id}", id);
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 删除维修单
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> DeleteRepair(int id)
        {
            try
            {
                await _repairService.DeleteAsync(id);
                _logger.LogInformation("删除维修单成功，ID: {Id}", id);
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除维修单失败，ID: {Id}", id);
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 生成维修单编号
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GenerateRepairCode()
        {
            try
            {
                var code = await _repairService.GenerateRepairCodeAsync();
                return Json(new { success = true, data = code });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "生成维修单编号失败");
                return Json(new { success = false, message = ex.Message });
            }
        }
    }
} 